package com.fyzk.activity.lottery.jc.football.guoguan;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.lottery.jc.football.JZBetNumUtil;
import com.fyzk.utils.IntentParamUtils;
import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.List;

public class JC_GuoGuanDialog extends Activity implements OnClickListener, OnCheckedChangeListener {

	private final String TAG = this.getClass().getSimpleName();

	public RadioButton rb_ziyou, rb_danguan;

	private LinearLayout ll_ziYou1, ll_ziYou2;
	private LinearLayout ll_duoChuan, ll_duoChuan2, ll_duoChuan3, ll_duoChuan4, ll_duoChuan5, ll_duoChuan6, ll_duoChuan7, ll_duoChuan8;
	private TextView tv_no, tv_ok;

	private List<RadioButton> list_r = new ArrayList<RadioButton>();
	private List<CheckBox> list_c = new ArrayList<CheckBox>();

	private int changCi;
	private int maxGuoGuan;
	
	public static final String GG_RESULT = "请选择过关方式";
	public static String result = "2串1";

	// 多串过关拆分的字符串，例如4场比赛3串4过关
	// 则3串4就是 3注2串1 1注3串1 duo_result = "2串1,3串1"
	public static String duo_result = "2串1";
	// 多串过关 M串n 时的M
	public static int team_num;
	// 是否是多串过关
	public static boolean isDuoChuan = false;

	private boolean isDanGuan = false;
	private static int lotteryID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.jc_guoguanstyle);

		changCi = getIntent().getIntExtra(IntentParamUtils.JC_CHANGCI, 1);
		maxGuoGuan = getIntent().getIntExtra(IntentParamUtils.JC_MAXGUOGUAN, 4);
		lotteryID = getIntent().getIntExtra(IntentParamUtils.LOTTERYID, 3001);

		if (changCi > maxGuoGuan) {
			changCi = maxGuoGuan;
		}

		info();
		
		if(Build.VERSION.SDK_INT >= 11 ){
			setFinishOnTouchOutside(false);
		}else{
			
		}
	}

	/**
	 * 控件初始化
	 */
	private void info() {

		rb_ziyou = findViewById(R.id.jc_guoguan_ziyou);
		rb_danguan = findViewById(R.id.jc_guoguan_danguan);

		rb_ziyou.setOnCheckedChangeListener(this);

		rb_danguan.setClickable(false);
		rb_danguan.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					AppToast.getToast().show("暂不开放单关");
				}
				return false;
			}
		});

		ll_ziYou1 = findViewById(R.id.jc_gg_ll_ziyou1);
		ll_ziYou2 = findViewById(R.id.jc_gg_ll_ziyou2);

		ll_duoChuan = findViewById(R.id.jc_gg_ll_duochuan);
		ll_duoChuan2 = findViewById(R.id.jc_gg_ll_duochuan2);
		ll_duoChuan3 = findViewById(R.id.jc_gg_ll_duochuan3);
		ll_duoChuan4 = findViewById(R.id.jc_gg_ll_duochuan4);
		ll_duoChuan5 = findViewById(R.id.jc_gg_ll_duochuan5);
		ll_duoChuan6 = findViewById(R.id.jc_gg_ll_duochuan6);
		ll_duoChuan7 = findViewById(R.id.jc_gg_ll_duochuan7);
		ll_duoChuan8 = findViewById(R.id.jc_gg_ll_duochuan8);

		tv_no = findViewById(R.id.jc_gg_no);
		tv_ok = findViewById(R.id.jc_gg_ok);

		addCheckBox(changCi);
		addRadioButton(changCi);

		// 事件
		tv_no.setOnClickListener(this);
		tv_ok.setOnClickListener(this);

		initView();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.jc_gg_no:
			setResult(IntentParamUtils.CANCLE);
			finish();
			break;
		case R.id.jc_gg_ok:
			String chuanStr = getResult();
			if(!"请选择过关方式".equals(chuanStr)){
				if (chuanStr.contains(",") || chuanStr.endsWith("串1")){
					JC_GuoGuanDialog.isDuoChuan = false;
				}else{
					JC_GuoGuanDialog.isDuoChuan = true;
					String[] split = chuanStr.split("串");
					int teamNum = Integer.valueOf(split[0]);
					int select = Integer.valueOf(split[1]);
					JC_GuoGuanDialog.team_num = teamNum;
					JC_GuoGuanDialog.duo_result = JZBetNumUtil.getChuanResult(teamNum, select);
				}
				JC_GuoGuanDialog.result = chuanStr;
				setResult(IntentParamUtils.OK);
				finish();
			}else{
				AppToast.getToast().show(chuanStr);
			}
			break;
		default:
			break;
		}
	}

	/**
	 * 获得checkbox控件
	 * 
	 * @param weight
	 * @param changCi
	 * @param isShow
	 * @return
	 */
	private View getCheckBox(float weight, int changCi, boolean isShow) {
		View view = LayoutInflater.from(this).inflate(R.layout.jc_guoguan_checkbox, null);
		final CheckBox c = view.findViewById(R.id.jc_gg_check);
		c.setText(changCi + "串1");

		list_c.add(c);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, weight);

		c.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				clearRadio();
			}
		});

		view.setLayoutParams(lp);

		if (!isShow) {
			view.setVisibility(View.INVISIBLE);
		}

		return view;
	}

	/**
	 * 添加checkbox控件
	 * 
	 * @param changCi
	 */
	private void addCheckBox(int changCi) {
		ll_ziYou2.removeAllViews();
		if (changCi > 1 && changCi < 6) {
			for (int i = 2; i < 6; i++) {
				ll_ziYou1.addView(getCheckBox(1, i, (i <= changCi)));
			}
		} else if (changCi >= 6) {
			for (int i = 2; i < 6; i++) {
				ll_ziYou1.addView(getCheckBox(1, i, (i <= changCi)));
			}

			for (int i = 6; i < 10; i++) {
				ll_ziYou2.addView(getCheckBox(1, i, (i <= changCi && i <= 8)));
			}
		}
	}

	/**
	 * 获得radiobutton控件
	 * @param weight
	 * @param n1
	 * @param n2
	 * @param isShow
	 * @return
	 */
	private View getRadioButton(float weight, int n1, int n2, boolean isShow) {
		View view = LayoutInflater.from(this).inflate(R.layout.jc_guoguan_radio, null);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, weight);

		final RadioButton r = (RadioButton) view.findViewById(R.id.jc_gg_radio);
		r.setText(n1 + "串" + n2);
		list_r.add(r);
		r.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				clearCheckBox();
				clearRadio();
				r.setChecked(true);
			}
		});

		view.setLayoutParams(lp);

		if (!isShow) {
			view.setVisibility(View.INVISIBLE);
		}

		return view;
	}

	/**
	 * 添加radiobutton控件
	 * 
	 * @param changCi
	 */
	private void addRadioButton(int changCi) {
		int[][] n_n = JC_GuoGuanInfor.n_n;

		for (int i = 0; i < n_n.length; i++) {

			switch (i / 4) {
			case 0:

				ll_duoChuan.addView(getRadioButton(1, n_n[i][0], n_n[i][1], (n_n[i][0] <= changCi)));
				break;
			case 1:
				ll_duoChuan2.addView(getRadioButton(1, n_n[i][0], n_n[i][1], (n_n[i][0] <= changCi)));
				break;
			case 2:
				ll_duoChuan3.addView(getRadioButton(1, n_n[i][0], n_n[i][1], (n_n[i][0] <= changCi)));
				break;
			case 3:
				ll_duoChuan4.addView(getRadioButton(1, n_n[i][0], n_n[i][1], (n_n[i][0] <= changCi)));
				break;
			case 4:
				ll_duoChuan5.addView(getRadioButton(1, n_n[i][0], n_n[i][1], (n_n[i][0] <= changCi)));
				break;
			case 5:
				ll_duoChuan6.addView(getRadioButton(1, n_n[i][0], n_n[i][1], (n_n[i][0] <= changCi)));
				break;
			case 6:
				ll_duoChuan7.addView(getRadioButton(1, n_n[i][0], n_n[i][1], (n_n[i][0] <= changCi)));
				break;
			case 7:
				ll_duoChuan8.addView(getRadioButton(1, n_n[i][0], n_n[i][1], (n_n[i][0] <= changCi)));
				break;
			default:
				break;
			}
		}
	}

	/**
	 * 初始化单、复选框
	 */
	private void initView() {
		if (result.contains(",")) {
			String[] rs = result.split(",");
			for (int i = 0; i < list_c.size(); i++) {
				for (int j = 0; j < rs.length; j++) {
					if (rs[j].equals(list_c.get(i).getText().toString())) {
						list_c.get(i).setChecked(true);
					}
				}
			}

			for (int i = 0; i < list_r.size(); i++) {
				for (int j = 0; j < rs.length; j++) {
					if (rs[j].equals(list_r.get(i).getText().toString())) {
						list_r.get(i).setChecked(true);
					}
				}
			}
		} else {
			for (int i = 0; i < list_c.size(); i++) {
				if (result.equals(list_c.get(i).getText().toString())) {
					list_c.get(i).setChecked(true);
				}
			}

			for (int i = 0; i < list_r.size(); i++) {
				if (result.equals(list_r.get(i).getText().toString())) {
					list_r.get(i).setChecked(true);
				}
			}
		}
	}

	/**
	 * 清除复选框
	 */
	private void clearCheckBox() {
		for (int i = 0; i < list_c.size(); i++) {
			if (list_c.get(i).isChecked()) {
				list_c.get(i).setChecked(false);
			}
		}
	}

	/**
	 * 清除单选框
	 */
	private void clearRadio() {
		for (int i = 0; i < list_r.size(); i++) {
			if (list_r.get(i).isChecked()) {
				list_r.get(i).setChecked(false);
			}
		}
	}

	/**
	 * 获取结果
	 * 
	 * @return
	 */
	private String getResult() {
		String r = "";
		if (isDanGuan) {
			r = "单关";
		} else {

			for (int i = 0; i < list_c.size(); i++) {
				if (list_c.get(i).isChecked()) {
					r += list_c.get(i).getText() + ",";
				}
			}
			if (r.length() > 0) {
				r = r.substring(0, r.length() - 1);// 去除最后一个“,”
			}
			for (int i = 0; i < list_r.size(); i++) {
				if (list_r.get(i).isChecked()) {
					r = (String) list_r.get(i).getText();
				}
			}
		}
		if(r.equals("")){
			r = "请选择过关方式";
		}
		return r;
	}

	public static void setResult1(int c) {
		if (c == 1) {
			return;
		}else if (c == 0){
			result = "请选择过关方式";
			return;
		}
		String r = "";
		if (result.contains(",")) {
			String[] str = result.split(",");
			for (int i = 0; i < str.length; i++) {
				if (str[i].contains("串")) {
					String[] s1 = str[i].split("串");
					int x = 0;
					try {
						x = Integer.parseInt(s1[0]);
						if (x <= c) {
							r += str[i] + ",";
						}
					} catch (Exception e) {
						result = "error";
					}
				}
			}
			if (r.length() > 0) {
				r = r.substring(0, r.length() - 1);// 去除最后一个“,”
			}
			result = r;
		} else {
			if (result.contains("串")) {
				String[] s2 = result.split("串");
				if (!s2[0].equals(c + "")) {
					result = "2串1";
				}
			}
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if (buttonView == rb_ziyou) {
			if (isChecked) {
				addCheckBox(changCi);
			}
		} else {
			if (isChecked) {
				ll_ziYou1.removeAllViews();
				ll_ziYou2.removeAllViews();
				TextView view = (TextView) View.inflate(this, R.layout.dj_text, null);
				isDanGuan = true;
				view.setText("已选择单关投注");
				ll_ziYou2.addView(view);
			} else {
				isDanGuan = false;
			}
		}
	}

}
